package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.PpdaiService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwPpdPush;
import com.meirengu.webview.model.KwPpdPushLog;
import com.meirengu.webview.service.KwPpdPushLogService;
import com.meirengu.webview.service.KwPpdPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 平安投放
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class PpdaiserviceImpl implements PpdaiService {

    Logger logger = LoggerFactory.getLogger(PpdaiserviceImpl.class);
    @Autowired
    private KwPpdPushService kwPpdPushService;

    @Autowired
    private KwPpdPushLogService kwPpdPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String phone, String name, String idcard, String city) throws Exception {

        String url = ConfigUtil.getConfig("ppdai.url");
        String channel = ConfigUtil.getConfig("ppdai.channel");
        String source = ConfigUtil.getConfig("ppdai.source");
        String token = ConfigUtil.getConfig("ppdai.token");

        Map<String, String> params = new HashMap<>();
        params.put("ChannelId", channel);
        params.put("SourceId", source);
        params.put("token", token);
        String paramMd5Str = MD5Util.get32MD5("token=" + token + "&phone=" + phone);
        String sign = MD5Util.get32MD5("token=" + token + "&phone=" + phone + "&paramMd5=" + paramMd5Str);
        params.put("sign", sign);
        params.put("phone", phone);
        params.put("userName", name);
        params.put("shenfenzh", idcard);
        params.put("applyLoanAmount", "4000");
        params.put("applyLoanMonth", "7");
        params.put("daikuanyt", "消费分期");
        JSONObject json = new JSONObject();
        json.put("applyCity", city);
        params.put("info", json.toJSONString());


        //接口调用信息表
        KwPpdPush kwPpdPush = new KwPpdPush();
        kwPpdPush.setInterfaceCode("ppdai");
        kwPpdPush.setInterfaceName("拍拍贷");
        kwPpdPush.setMobile(phone);
        kwPpdPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwPpdPushLog kwPpdPushLog = new KwPpdPushLog();
        kwPpdPushLog.setInterfaceCode("ppdai");
        kwPpdPushLog.setInterfaceName("拍拍贷");
        kwPpdPushLog.setMobile(phone);
        kwPpdPushLog.setSendUrl(url.toString());
        kwPpdPushLog.setParams(params.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwPpdPushLog.setBackUrl(hr.toString());
                kwPpdPushLog.setHttpCode(200);
                kwPpdPushLog.setHttpContent(hr.toString());

                logger.info("==PpdaiserviceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("Code");
                String rMsg = returnMsg.getString("Msg");
                if ("1".equals(rCode)) {
                    kwPpdPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwPpdPush.setMessage("成功");
                    logger.info("==PpdaiserviceImpl.applyLoan business success");
                } else {
                    kwPpdPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwPpdPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==PpdaiserviceImpl.applyLoan business fail");
                }
            } else {
                kwPpdPushLog.setBackUrl(hr.toString());
                kwPpdPushLog.setHttpCode(code);
                kwPpdPushLog.setHttpContent(hr.toString());
                logger.warn("==PpdaiserviceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwPpdPushLog.setHttpCode(408);
            kwPpdPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwPpdPushService.insert(kwPpdPush);
            kwPpdPushLog.setPushId(kwPpdPush.getId());
            kwPpdPushLogService.insert(kwPpdPushLog);
            return kwPpdPush.getCode();
        }
    }

}
